https://github.com/irJERAD/AppleHealthDashboard
Here you can see a Pin of my home in San Francisco!
Interactive panning/zooming
Compose maps using arbitrary combinations of map tiles, markers, polygons, lines, popups, and GeoJSON.
Create maps right from the R console or RStudio
Embed maps in knitr/R Markdown documents and Shiny apps
Easily render Spatial objects from the sp package, or data frames with latitude/longitude columns
Use map bounds and mouse events to drive Shiny logic
Here I used the ggplot2 libraries geometric tile plotting ability to create a heat map of my walking data
With 2017 only just starting (I am writing this at 10am on Jan 5th and only started last evening). A boxplot does really help much.
I considered filtering out this new year. However I am determined to impliment an automation that will continue to take my exported data and update this page - mostly because this is a static site and I love getting as much as possible from as little as necessary
Lessons on Plotly + ggplot2 = ggplotly():
---
title: "Apple Health Dashboard"
output:
flexdashboard::flex_dashboard:
storyboard: true
social: menu
source: embed
---
```{r setup, include=FALSE, cache=TRUE}
# This script will be shown in the source code
# but hidden from the dashboard for practical purposes
# upload libraries to be used in any of the code blocks at once ahead of time
library(flexdashboard)
library(dplyr)
library(ggplot2)
library(lubridate)
library(XML)
library(plotly)
library(wesanderson)
# Set path to export
exportPath <- "/Users/irJERAD/AppleHeathDashboard/apple_health_export/export.xml"
#load apple health export.xml file
xml <- xmlParse(exportPath)
#transform xml file to data frame - select the Record rows from the xml file
df <- XML:::xmlAttrsToDataFrame(xml["//Record"])
#make value variable numeric
## TODO fix error NAs introduced by coercion (looks like a single extra row is added)
df$value <- as.numeric(as.character(df$value))
#make endDate in a date time variable POSIXct using lubridate with eastern time zone
df$endDate <-ymd_hms(df$endDate,tz="America/Los_Angeles")
##add in year month date dayofweek hour columns
df$month<-format(df$endDate,"%m")
df$year<-format(df$endDate,"%Y")
df$date<-format(df$endDate,"%Y-%m-%d")
df$dayofweek <-wday(df$endDate, label=TRUE, abbr=FALSE)
df$hour <-format(df$endDate,"%H")
# create sub data frames for quicker interaction
heart <- df %>% filter(type == 'HKQuantityTypeIdentifierHeartRate')
steps <- df %>% filter(type == 'HKQuantityTypeIdentifierStepCount')
```
### Leaflet is a JavaScript library for creating dynamic maps that support panning and zooming along with various annotations.
```{r map of home}
library(leaflet)
leaflet() %>%
addTiles() %>%
addMarkers(lng=-122.4125580, lat=37.7781400, popup="Current Residence of Jerad")
```
***
https://github.com/irJERAD/AppleHealthDashboard
- Here you can see a Pin of my home in San Francisco!
- Interactive panning/zooming
- Compose maps using arbitrary combinations of map tiles, markers, polygons, lines, popups, and GeoJSON.
- Create maps right from the R console or RStudio
- Embed maps in knitr/R Markdown documents and Shiny apps
- Easily render Spatial objects from the sp package, or data frames with latitude/longitude columns
- Use map bounds and mouse events to drive Shiny logic
### ggplot2 - while well known and used - has a very diverse set of plot types; while old can be boring the selection is indispensable to data exploration and discovery.
```{r heatmap steps}
df %>%
filter(type == 'HKQuantityTypeIdentifierStepCount') %>%
group_by(date,dayofweek,hour) %>%
summarize(steps=sum(value)) %>%
group_by(hour,dayofweek) %>%
summarize(steps=sum(steps)) %>%
arrange(desc(steps)) %>%
#print table steps by date by month by year
ggplot(aes(x=dayofweek, y=hour, fill=steps)) +
geom_tile() +
scale_fill_continuous(labels = scales::comma, low = 'white', high = 'red') +
theme_bw() +
theme(panel.grid.major = element_blank())
```
***
Here I used the [ggplot2](https://github.com/hadley/ggplot2-book) libraries geometric tile plotting ability to create a heat map of my walking data
- Week Days around 6pm were the busiest time
- This makes sense since that is when I am walking home from work
- This Library was written by one of my favorite data scientists
### More ggplot this time box plot - should make this a lattice
```{r step by step}
df %>%
filter(type == 'HKQuantityTypeIdentifierStepCount') %>%
group_by(dayofweek,date,year) %>%
summarize(steps=sum(value)) %>%
#print table steps by date by month by year
ggplot(aes(x=dayofweek, y=steps)) +
geom_boxplot(aes(fill=(year))) +
# uses nice default blue-ish palette
scale_fill_brewer() +
# sets background theme to my rediculous standards
# Note: still not completely satisfied but productivity beckons me
theme(panel.grid.major.x = element_blank(), panel.grid.minor =
element_blank(), panel.grid.major.y = element_line(color = "gray",
size = 0.2),
panel.background = element_rect(color = "black", fill = "#fbfbfb")) + scale_fill_brewer()
```
***
With 2017 only just starting (I am writing this at 10am on Jan 5th and only started last evening). A boxplot does really help much.
I considered filtering out this new year. However I am determined to impliment an automation that will continue to take my exported data and update this page - mostly because this is a static site and I love getting as much as possible from as little as necessary
### Plotly provides bindings to the plotly.js library and allows you to easily translate your ggplot2 graphics into an interactive web-based version.
```{r ggplot + Plotly}
# plotly with ggplot2 for interactive graphics
h <- heart %>%
# text is the tool tip
ggplot(aes(x = date, y = value,
# create custome hover tooltip variables
text = (paste("BPM:", value,
"
Source:", sourceName,
"
Date", date)))) +
# Color and implied key
geom_point(aes(colour=sourceName)) +
scale_color_manual(name = "Source of
Collected Data", values = wes_palette(n=5, name = "Darjeeling")) +
labs(title = "My Heart Rate From April 13th, 2015 through current data Export Jan 5th 2017",
x = "2015 --- Date --- 2017", y = "Beats per min") +
theme(plot.title = element_text(size = 10, color = "#FF0000", face = "bold"),
legend.text = element_text(size = 6, colour = "#00c8ff", face = "italic"),
legend.title = element_text(size = 13, colour = "#00A08A", face = "italic"),
# legend.background = element_rect(), legend.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"),
# legend.key.size = unit(1, "cm"),
axis.title.x = element_text(size = 12, colour = "#ffffff"),
axis.title.y = element_text(size = 12, colour = "#ffffff"),
plot.background = element_rect(fill = "#5BBCD6"))
ggplotly(h, tooltip = "text")
```
***
**Lessons on Plotly + ggplot2 = ggplotly():**
- I definitely got a little over zealous on this one
- So many colors, too many colors
- Did I really need all those colors?
- I hope you got a chuckle out of those colors because I know they didn't add anything of substance
- I was WAY over committed
- adding JavaScript, HTML, and interactive elements to my favorite plotting tool, of course I would get a little... obsessed
- This added a lot of time to things I could have just done using plotly alone and gotten faster and better results, but that's a cost to knowledge I am well acquainted with and one I respect
- I should just make Plotly my go to library for interactive graphics
- This thing is *way* cool
- sorry Hadley ;(